import { fileURLToPath, URL } from 'node:url'
import viteCompression from 'vite-plugin-compression'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Unocss from 'unocss/vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import basicSsl from '@vitejs/plugin-basic-ssl'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, './env')
  return {
    base: env.VITE_BASE_URL,
    envDir: './env',
    plugins: [
      vue(),
      vueJsx(),
      Unocss(),
      viteCompression({
        verbose: true, // 默认即可
        disable: false, //开启压缩(不禁用)，默认即可
        deleteOriginFile: false, //删除源文件
        threshold: 10240, //压缩前最小文件大小
        algorithm: 'gzip', //压缩算法
        ext: '.gz' //文件类型
      }),
      createHtmlPlugin({
        minify: true,
        inject: {
          data: {
            injectMeta:
              mode !== 'pro'
                ? ''
                : `<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />`
          }
        }
      }),
      basicSsl({
        name: 'dev',
        /** custom trust domains */
        domains: ['*.liangfangsz.com'],
        certDir: './cert'
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@common': 'vue3-common/dist/vue3-common.umd',
        '@common-css': 'vue3-common/dist/style.css'
      }
    },
    server: {
      host: true,
      port: 8001,
      cors: true,
      https: true,
      proxy: {
        '/api': {
          target: 'https://flow.btvs.cn',
          changeOrigin: true // 是否允许跨域
          // rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  }
})
